package com.ruoyi.product.service.impl;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.db.domain.AccountProductInfo;
import com.ruoyi.db.domain.ProductBaseInfo;
import com.ruoyi.db.mapper.AccountProductInfoMapper;
import com.ruoyi.db.mapper.ProductBaseInfoMapper;
import com.ruoyi.db.mapper.ProductProfitInfoMapper;
import com.ruoyi.db.model.ProductOnlieInfoParam;
import com.ruoyi.laiqianba.constant.RedisKeyConstant;
import com.ruoyi.laiqianba.service.IHandleCacheService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.db.mapper.ProductOnlieInfoMapper;
import com.ruoyi.db.domain.ProductOnlieInfo;
import com.ruoyi.product.service.IProductOnlieInfoService;

import javax.annotation.Resource;

import static java.util.stream.Collectors.toList;

/**
 * 产品上线信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-09-04
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class ProductOnlieInfoServiceImpl implements IProductOnlieInfoService 
{
    @Resource
    private ProductOnlieInfoMapper productOnlieInfoMapper;
    @Resource
    private ProductBaseInfoMapper productBaseInfoMapper;
    @Autowired
    private IHandleCacheService handleCacheService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    ProductProfitInfoMapper  productProfitInfoMapper;

    /**
     * 查询产品上线信息
     * 
     * @param productId 产品上线信息主键
     * @return 产品上线信息
     */
    @Override
    public ProductOnlieInfo selectProductOnlieInfoByProductId(Long productId)
    {
        return productOnlieInfoMapper.selectProductOnlieInfoByProductId(productId);
    }

    /**
     * 查询产品上线信息列表
     * 
     * @param productOnlieInfo 产品上线信息
     * @return 产品上线信息
     */
    @Override
    public List<ProductOnlieInfo> selectProductOnlieInfoList(ProductOnlieInfo productOnlieInfo)
    {
        return productOnlieInfoMapper.selectProductOnlieInfoList(productOnlieInfo);
    }

    /**
     * 查询产品上线信息列表
     *
     * @param productOnlieInfoParam 产品上线信息
     * @return 产品上线信息
     */
    @Override
    public List<Map<String, Object>> selectProductOnlieInfoMapList(ProductOnlieInfoParam productOnlieInfoParam)
    {
        return productOnlieInfoMapper.selectProductOnlieInfoMapList(productOnlieInfoParam);
    }

    /**
     * 新增产品上线信息
     * 
     * @param productOnlieInfo 产品上线信息
     * @return 结果
     */
    @Override
    public AjaxResult insertProductOnlieInfo(ProductOnlieInfo productOnlieInfo)
    {

        String inChannelArray = productOnlieInfo.getInChannelArray();
        if(StringUtils.equals(inChannelArray,"[]")){
            productOnlieInfo.setInChannelArray(null);
        }
        String outChannelArray = productOnlieInfo.getOutChannelArray();
        if(StringUtils.equals(outChannelArray,"[]")){
            productOnlieInfo.setOutChannelArray(null);
        }
        String notPushDayWeek = productOnlieInfo.getNotPushDayWeek();
        if(StringUtils.equals(notPushDayWeek,"[]")){
            productOnlieInfo.setNotPushDayWeek(null);
        }
        String baoxian = productOnlieInfo.getBaoxian();
        if(StringUtils.equals(baoxian,"[]")){
            productOnlieInfo.setBaoxian(null);
        }
        String baitiao = productOnlieInfo.getBaitiao();
        if(StringUtils.equals(baitiao,"[]")){
            productOnlieInfo.setBaitiao(null);
        }
        String car = productOnlieInfo.getCar();
        if(StringUtils.equals(car,"[]")){
            productOnlieInfo.setCar(null);
        }
        String house = productOnlieInfo.getHouse();
        if(StringUtils.equals(house,"[]")){
            productOnlieInfo.setHouse(null);
        }
        String gjj = productOnlieInfo.getGjj();
        if(StringUtils.equals(gjj,"[]")){
            productOnlieInfo.setGjj(null);
        }
        String shebao = productOnlieInfo.getShebao();
        if(StringUtils.equals(shebao,"[]")){
            productOnlieInfo.setShebao(null);
        }
        String gongzuo = productOnlieInfo.getGongzuo();
        if(StringUtils.equals(gongzuo,"[]")){
            productOnlieInfo.setGongzuo(null);
        }
        String huabei = productOnlieInfo.getHuabei();
        if(StringUtils.equals(huabei,"[]")){
            productOnlieInfo.setHuabei(null);
        }
        String loanAmount = productOnlieInfo.getLoanAmount();
        if(StringUtils.equals(loanAmount,"[]")){
            productOnlieInfo.setLoanAmount(null);
        }
        String loanTime = productOnlieInfo.getLoanTime();
        if(StringUtils.equals(loanTime,"[]")){
            productOnlieInfo.setLoanTime(null);
        }
        String orFilter = productOnlieInfo.getOrFilter();
        if(StringUtils.equals(orFilter,"[]")){
            productOnlieInfo.setOrFilter(null);
        }
        String shouru = productOnlieInfo.getShouru();
        if(StringUtils.equals(shouru,"[]")){
            productOnlieInfo.setShouru(null);
        }
        String xueli = productOnlieInfo.getXueli();
        if(StringUtils.equals(xueli,"[]")){
            productOnlieInfo.setXueli(null);
        }
        String zhima = productOnlieInfo.getZhima();
        if(StringUtils.equals(zhima,"[]")){
            productOnlieInfo.setZhima(null);
        }
        String yuqi = productOnlieInfo.getYuqi();
        if(StringUtils.equals(yuqi,"[]")){
            productOnlieInfo.setYuqi(null);
        }
        String yongtu = productOnlieInfo.getYongtu();
        if(StringUtils.equals(yongtu,"[]")){
            productOnlieInfo.setYongtu(null);
        }
        String cityInfo = productOnlieInfo.getCityInfo();
        if(StringUtils.equals(cityInfo,"[]")){
            productOnlieInfo.setCityInfo(null);
        }
        String outCityArray = productOnlieInfo.getOutCityArray();
        if(StringUtils.equals(outCityArray,"[]")){
            productOnlieInfo.setOutCityArray(null);
        }
        //通过产品id查询出基础表中的产品名称,放入对象中
        ProductBaseInfo productBaseInfo = productBaseInfoMapper.selectProductBaseInfoById(productOnlieInfo.getProductId());
        productOnlieInfo.setProductName(productBaseInfo.getProductName());

        productOnlieInfo.setCreateTime(DateUtils.getNowDate());
        return AjaxResult.success(productOnlieInfoMapper.insertProductOnlieInfo(productOnlieInfo));
    }

    /**
     * 修改产品上线信息
     * 
     * @param productOnlieInfo 产品上线信息
     * @return 结果
     */
    @Override
    public int  updateProductOnlieInfo(ProductOnlieInfo productOnlieInfo)
    {

        String inChannelArray = productOnlieInfo.getInChannelArray();
        if(StringUtils.equals(inChannelArray,"[]")){
            productOnlieInfo.setInChannelArray(null);
        }
        String outChannelArray = productOnlieInfo.getOutChannelArray();
        if(StringUtils.equals(outChannelArray,"[]")){
            productOnlieInfo.setOutChannelArray(null);
        }
        String notPushDayWeek = productOnlieInfo.getNotPushDayWeek();
        if(StringUtils.equals(notPushDayWeek,"[]")){
            productOnlieInfo.setNotPushDayWeek("[]");
        }
        if(productOnlieInfo.getMaxAge() == 0){
            productOnlieInfo.setMaxAge(null);
        }
        if(productOnlieInfo.getMinAge() == 0){
            productOnlieInfo.setMinAge(null);
        }
        if (productOnlieInfo.getUvCount() != null){
            String dateInfo = LocalDate.now().toString();

            productOnlieInfoMapper.selectProductOnlieInfoList(productOnlieInfo);
            productProfitInfoMapper.updateOnlieById(dateInfo,productOnlieInfo.getUvCount(),productOnlieInfo.getProductId());
            String redisKey = StrUtil.format(RedisKeyConstant.PRODUCT_UV_REMAIN_COUNT_KEY, dateInfo, productOnlieInfo.getProductId());
            stringRedisTemplate.delete(redisKey);
        }

        //通过产品id查询出基础表中的产品名称,放入对象中
        ProductBaseInfo productBaseInfo = productBaseInfoMapper.selectProductBaseInfoById(productOnlieInfo.getProductId());
        productOnlieInfo.setProductName(productBaseInfo.getProductName());

        productOnlieInfo.setUpdateTime(DateUtils.getNowDate());
        int i = productOnlieInfoMapper.updateProductOnlieInfo(productOnlieInfo);
        if (i>0){
            String dateInfo = LocalDate.now().toString();
            handleCacheService.clearProductRemainNumber(String.valueOf(productOnlieInfo.getProductId()), dateInfo);
        }

        return i;
    }
    @Override
    public int  updateStatus(ProductOnlieInfo productOnlieInfo)
    {
        int i = productOnlieInfoMapper.updateStatus(productOnlieInfo);
        if (i>0){
            String dateInfo = LocalDate.now().toString();
            handleCacheService.clearProductRemainNumber(String.valueOf(productOnlieInfo.getProductId()), dateInfo);
        }

        return i;
    }

    /**
     * 删除产品上线信息
     * 
     * @param productIds 需要删除的产品上线信息主键
     * @return 结果
     */
    @Override
    public int deleteProductOnlieInfoByProductIds(Long[] productIds)
    {
        if (productIds.length != 0){
            return productOnlieInfoMapper.deleteProductOnlieInfoByProductId(Arrays.asList(productIds).get(0));
        }else {
            return 0;
        }

    }

    /**
     * 删除产品上线信息信息
     * 
     * @param productId 产品上线信息主键
     * @return 结果
     */
    @Override
    public int deleteProductOnlieInfoByProductId(Long productId)
    {
        return productOnlieInfoMapper.deleteProductOnlieInfoByProductId(productId);
    }

    /**
     * 修改产品上线状态
     */
    @Override
    public AjaxResult updateProductOnlieStatusById(Long id, Long status) {
        int i = productOnlieInfoMapper.updateProductOnlieStatusById(id, status);
        if (i > 0){
            if (status == 0){
                return AjaxResult.success("停用成功");
            }
            if (status == 1){
                return AjaxResult.success("启用成功");
            }
        }
        return AjaxResult.error("操作失败");
    }

    /**
     * 一键置顶
     * */
    @Override
    public AjaxResult updateOneClickToTopById(Long id) {
        int i = productOnlieInfoMapper.updateOneClickToTopById(id);
        if (i > 0){
                return AjaxResult.success("一键置顶成功");
        }
        return AjaxResult.error("一键置顶失败");
    }

    /**
     * 取消置顶
     * */
    @Override
    public AjaxResult updateCancelToppingById(Long id) {
        int i = productOnlieInfoMapper.updateCancelToppingById(id);
        if (i > 0){
            return AjaxResult.success("取消置顶成功");
        }
        return AjaxResult.error("取消置顶失败");
    }

    /**
     * 新增未上架的产品ID
     * 管理员可以上架所有的产品
     */
    @Override
    public List<Map<String, Object>> inProductNameList(Long userId) {

        if (SecurityUtils.loginUserIsAdmin()){
            userId = null;
        }
        List<Map<String, Object>> intList = productOnlieInfoMapper.unLineProductMapList(userId);
        return intList;
    }

    @Override
    public int updateJumpStatus(ProductOnlieInfo productOnlieInfo) {
       return productOnlieInfoMapper.updateJumpStatus(productOnlieInfo);
    }

    @Override
    public List<Map<String, Object>> productListBySort() {
        return productOnlieInfoMapper.productListBySort();
    }

}
